<?php
namespace App\Services\util;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/12/9
 * Time: 16:49
 */

class HttpUtil
{

    /**
     * NO CACHE 文件头
     *
     * @param $type
     * @param $charset
     */
    public static function no_cache_header($type = 'text/html', $charset = 'utf-8')
    {
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
        header('Pragma: no-cache');
        header('Content-Type: ' . $type . '; charset=' . $charset . '');
    }

    public static function parse_redirect_url($url)
    {
        if (substr($url, 0, 1) == '?') {
            $url = base_url() . $url;
        } else if (substr($url, 0, 1) == '/') {
            $url = get_js_url($url);
        }

        return $url;
    }

    public static function redirect($url)
    {
        if ($url = HTTP::parse_redirect_url($url)) {
            header('Location: ' . $url);
            die;
        }
    }



    public static function request($url, $method, $post_fields = null, $time_out = 15, $header = null, $cookie = null)
    {

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl, CURLOPT_TIMEOUT, $time_out);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36');
        switch ($method) {
            case 'POST' :
                curl_setopt($curl, CURLOPT_POST, TRUE);

                if ($post_fields) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
                }
                break;

            case 'DELETE' :
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');

                if ($post_fields) {
                    $url = "{$url}?{$post_fields}";
                }
                break;
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);

        if (isset($header) AND !is_array($header)) {
            unset($header);
        }

        $header[] = 'API-RemoteIP: ' . fetch_ip();

        if ($header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }

        if (substr($url, 0, 8) == 'https://') {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        }

        if ($cookie AND is_array($cookie)) {
            curl_setopt($curl, CURLOPT_COOKIE, urldecode(http_build_query($cookie, '', '; ')));
        }

        $response = curl_exec($curl);

        curl_close($curl);

        return $response;
    }

}